import Vue from 'vue'
import Router from 'vue-router'

// 导入二级动态路由
import approvalsRouter from './modules/approvals'
import attendancesRouter from './modules/attendances'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 动态路由表，项目中不同的用户可以访问不同的功能
export const asyncRouter = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter, // 本期不实现功能
  attendancesRouter, // 本期不实现功能
  salarysRouter, // 本期不实现功能
  socialRouter // 本期不实现功能
]

// // 静态路由表，项目中每个用户都可以访问的功能
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  },

  {
    path: '/excel',
    component: Layout,
    hidden: true, // 不显示到左侧菜单
    children: [
      {
        path: '',
        component: () => import('@/views/excel')
      }
    ]
  }

  // // 404 页面
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 拼接动态路由
  // routes: [...constantRoutes, ...asyncRouter]
  routes: [...constantRoutes]
})

// 相当于重新生成一份路由实例 router
const router = createRouter()

// 重置路由, 在退出的时候调用一下这个方法就可以了
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
